<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="70" label-position="right">
        <FormItem label="公司名称" prop="company_name">
            <Input type="text" v-model="formValidate.company_name"></Input>
        </FormItem>
        <FormItem label="联系人">
            <Input type="text" v-model="formValidate.linkman"></Input>
        </FormItem>
        <FormItem label="联系电话">
            <Input type="text" v-model="formValidate.linkphone"></Input>
        </FormItem>
        <FormItem label="机构代码" prop="mechanism_code">
            <Input type="text" v-model="formValidate.mechanism_code"></Input>
        </FormItem>
        <FormItem label="地址">
            <Input type="text" v-model="formValidate.address"></Input>
        </FormItem>
        <FormItem label="结算方式">
            <RadioGroup v-model="formValidate.settlement">
                <Radio :label="1">月结</Radio>
                <Radio :label="2">半月结</Radio>
                <Radio :label="3">现结</Radio>
                <Radio :label="4">预存款</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="业务员">
            <Select placeholder="选择业务员" v-model="formValidate.salesman_id">
                <Option v-for="item in salesmans" :value="item.id" :key="item.id">{{ item.cn_name }}</Option>                
            </Select>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">确定</Button>
        </FormItem>
    </Form>
</template>
<script>

export default {
    name: 'clientinfo',
    props: ['formValidate'],
    data(){
        return {            
            salesmans:'',            
            ruleValidate:{
                company_name:[
                    { required: true, message: '请输入公司名称', trigger: 'blur' }
                ],
                mechanism_code:[
                    { required: true, message: '请输入机构代码', trigger: 'blur' }
                ]
            }       
        }
    },
    created(){        
        this.$axios({
            mothod: 'post',
            url: 'http://localhost/public/api/manager',
            params: ''
        }).then((response)=> { 
            this.salesmans = response.data.datas;
        })
        .catch(function (error) {
            console.log(error);
        }); 
    },
    methods: { 
        addClient(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/client/create',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('录入成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('录入失败!');
            });   
        },
        editClient(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/client/update',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('编辑成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('编辑失败!');
            });   
        },                 
        handleSubmit(name){                 
            this.$refs[name].validate((valid) => {                   
                if (valid) {                                 
                    if(this.formValidate.id != ""){
                        this.editClient(); 
                    }else{
                        this.addClient();                                 
                    }  
                    this.$emit('updateClientList');    
                    this.$emit('closeDialog');              
                } else {
                    this.$Message.error('提交失败!');
                }
            })
        }
    }
}
</script>
